<?php
/**
 * Author:Albert Zhan(http://www.5lazy.cn)
 * Date: 2017/4/1 0001
 * Time: 10:45
 */
namespace Weixin\Controller;
class IndexController extends \Common\Controller\BaseController{

    //根据userid获取用户ID存进会话
    public function _initialize(){
//        if(empty($_SESSION['user_id'])){
//            $userid='x1x1x1';
//            $rows=M('user')->where("userid='%s' AND status=%d",array($userid,0))->field('pk_id')->find();
//            $_SESSION['user_id']=$rows['pk_id'];
//        }
        if(empty($_SESSION['user_id'])){
            $WeixinQiye = new \Weixin\Controller\WeixinQiyeController();
            $WeixinQiye->oauth2_run();
        }
    }

    /**
     * 车牌号查询
     */
    public function SelectQuery(){
        if($_POST){
            $model=M('order');
            $rules=array(
                array('query_car_no','require','车牌号不能为空'),
                array('query_datetime','require','查询时间不能为空'),
            );
            $_POST['fk_user_id']=$_SESSION['user_id'];
            $_POST['order_no']=$this->createApprovalNo();
            $_POST['create_time']=date('Y-m-d H:i:s');
            if(!$model->validate($rules)->create()){
                $this->rtnResult(1,$model->getError());
            }else{
                if($model->add()){
                    $this->rtnResult(0,'查询成功');
                }else{
                    $this->rtnResult(1,'查询失败');
                }
            }
        }else{
            $this->display();
        }
    }

    /**
     * 车牌号列表
     */
    public function CarNoList(){
        $this->display();
    }

    /**
     * 获取车牌号列表
     */
    public function GetCarNoList(){
        $where['_string']='o.fk_user_id=u.pk_id';
        $where['o.fk_user_id']=$_SESSION['user_id'];
        $where['o.status']=0;
        $field="o.pk_id,o.query_car_no,o.query_datetime,o.stage,o.create_time,u.name";
        $rows=M()->table(array('order'=>'o','user'=>'u'))->page(I('post.page',1),10)->where($where)->field($field)->order('pk_id DESC')->select();
        $this->data['rows']=$rows;
        $this->rtnResult(0);
    }

    /**
     * 车牌号详情
     */
    public function CarNoInfo(){
        $pk_id=I('get.pk_id');
        !$pk_id && exit('参数不全');
        $where['_string']='o.fk_user_id=u.pk_id';
        $where['o.fk_user_id']=$_SESSION['user_id'];
        $where['o.pk_id']=$pk_id;
        $where['o.stage']=2;
        $where['o.status']=0;
        $field="o.pk_id,o.query_car_no,o.query_datetime,o.create_time,u.name";
        $rows=M()->table(array('order'=>'o','user'=>'u'))->where($where)->field($field)->find();
        if(empty($rows)){
            exit('非法操作');
        }else{
            $pic_data=M('finish_img')->where("order_id=%d AND status=%d",array($rows['pk_id'],0))->field('path')->select();
            $rows['pic_list']=$pic_data;
            $this->assign('url',__ROOT__);
            $this->assign('rows',$rows);
            $this->display();
        }
    }

}